import path from 'path';
import { defineConfig, loadEnv,ConfigEnv,UserConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite';
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers';

//纯静态开发测试时，使用mockjs虚拟异步返回
import { viteMockServe } from "vite-plugin-mock"
import { visualizer } from "rollup-plugin-visualizer"
import viteCompression from "vite-plugin-compression"

// https://vite.dev/config/
export default defineConfig(({command, mode}:ConfigEnv):UserConfig => {
	const env = loadEnv(mode, './env')
	console.log(env, command, mode)
	let plugins = []
	if (mode === 'mock') {
		//npm run dev 时引入mock
		plugins.push(
			viteMockServe({
				mockPath: "./mock/",
				//localEnabled: true,
				//prodEnabled: false,
				// injectCode: `
				//     import { setupMock } from './mock/index';
				//     setupMock();
				// `,
				// 我们不需要build打包后生产环境还能用，因此不需要做/mock目录下index.ts的特殊设计，也不需要做injectCode注入
				watchFiles: true, // 监听文件内容变更
			})
		)
	} else if (mode === "production") {
		// 打包依赖包大小展示
		plugins.push(
			visualizer({
				open: true,
				gzipSize: true,
				brotliSize: true,
			})
		)
	}
	return {
		plugins: [
			vue(),
			AutoImport({
				dts:true,
				dirs:['src/store','src/hooks','src/api','src/types'],
				imports:['vue','vue-router','pinia'],
				resolvers: [  AntDesignVueResolver({ importStyle: false,}),], // css in js
			}),
			Components({
				dts: true,
				dirs: ['src/components','src/layout'], // 按需自动加载的文件夹， 减少import自定义组件的代码编写量
				resolvers: [  AntDesignVueResolver({ importStyle: false,}),], // css in js
			}),
			// gizp压缩
			viteCompression({
				verbose: true,
				disable: false,
				threshold: 10240,
				algorithm: "gzip",
				ext: ".gz",
			}),
			...plugins,
		],
		envDir:'./env',
		resolve: {
			// 设置文件目录别名; 根目录地址变更，也需要调整
			alias: {
				'@': path.resolve(__dirname, './src'),
			},
		},
		// 构建配置项
		build: {
			target:'modules',
			//设置打包出来的静态资源存放目录
			assetsDir: "assets",
			// 启用、禁用css代码拆分
			cssCodeSplit: true,
			sourcemap: false, //是否构建source map 文件
			// 指定输出目录
			outDir: path.resolve(__dirname, 'dist'),
			emptyOutDir: true, //构建时默认先清空dist目录
			minify: 'terser', // 混淆器，terser构建后文件体积更小
			terserOptions: {
				// 生产环境下移除console
				compress: {
					drop_console: true,
					drop_debugger: true
				}
			},
		},
		// 本地运行配置，及反向代理配置
		server: {
			host: env.VITE_HOST, // 指定服务器主机名
			port: parseInt(env.VITE_PORT,10) || undefined, // 指定服务器端口
			open: true, // 在服务器启动时自动在浏览器中打开应用程序
			cors: true, // 允许跨域
			//设置代理
			proxy: {
				'/sso':{
					target:'http://localhost:8190',   //本机后端测试环境
					//target:'http://221.7.198.233:8180/api',   //本地连生产环境后端做调试
					changeOrigin:true,
					secure:false,
					rewrite:(path) => path.replace(/^\/sso/,'')
				},
				'/api':{
					target: 'http://localhost:8192',
					changeOrigin: true, // Allow cross-origin requests
					rewrite: path => path.replace(/^\/api/, ''),
				},
				// '/my-file': {
				// 	target: 'https://dev.gxjintu.com:8012/v1/files',
				// 	changeOrigin: true, // Allow cross-origin requests
				// 	rewrite: path => path.replace(/^\/my-file/, ''),
				// },
				'/my-preview': {
					target: 'https://dev.gxjintu.com:8012',
					changeOrigin: true, // Allow cross-origin requests
					rewrite: path => path.replace(/^\/my-preview/, ''),
				}
			},
			// proxy:{
			//     '/api':{
			//         target:'http://xxx.xxx.xxx:8000',
			//         changeOrigin:true,
			//         secure:false,
			//         rewrite:(path) => path.replace('/api/','/')
			//     }
			// }
		},
	}

})
